package com.laizhenghua.hotel.controller;

import com.laizhenghua.hotel.entity.ApplyEntity;
import com.laizhenghua.hotel.entity.HouseEntity;
import com.laizhenghua.hotel.entity.UsersEntity;
import com.laizhenghua.hotel.service.ApplyService;
import com.laizhenghua.hotel.service.HouseService;
import com.laizhenghua.hotel.service.UsersService;
import com.laizhenghua.hotel.utils.DateKit;
import com.laizhenghua.hotel.utils.MainUtil;
import com.laizhenghua.hotel.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * @description:
 * @author: laizhenghua
 * @date: 2021/3/21 13:24
 */
@RestController
@RequestMapping("hotel/app")
public class OrderController {
    @Autowired
    private ApplyService applyService;
    @Autowired
    private UsersService usersService;
    @Autowired
    private HouseService houseService;
    /**
     * 处理预订信息请求
     * API：http://localhost:8080/hotel/app/post
     * 请求参数：无
     */
    @PostMapping("/post")
    @Transactional
    public R ordering(@RequestBody Map<String,Object> params) throws ParseException {
        // System.out.println("debug=> " + params);
        int houseId = Integer.parseInt(params.get("id").toString());
        String inDate = params.get("indate").toString();
        String outDate = params.get("outdate").toString();
        Map<String,Object> map = new HashMap<>();
        if (this.checkTimeUsed(houseId,inDate,outDate)) {
            map.put("state","error");
            map.put("message","很抱歉，该时段已被预订!");
        }else {
            UsersEntity oldUsersEntity = usersService.getOneByTelAndUsername(params.get("username").toString(),params.get("tel"));
            boolean save = false;
            if(oldUsersEntity == null) {
                UsersEntity usersEntity = new UsersEntity();
                usersEntity.setUsername(params.get("username").toString());
                usersEntity.setSex(Integer.parseInt(params.get("sex").toString()));
                usersEntity.setTel(params.get("tel").toString());
                usersEntity.setGrade(1);
                usersEntity.setType(1);
                usersEntity.setCreated(MainUtil.getTime());
                save = usersService.save(usersEntity);
                applyService.saveByUserNameAndTel(params,usersEntity.getUsername(),usersEntity.getTel());
            } else {
                applyService.saveByUserNameAndTel(params,oldUsersEntity.getUsername(),oldUsersEntity.getTel());
                save = true;
            }
            if (save) {
                HouseEntity houseEntity = houseService.getById(Integer.parseInt(params.get("id").toString()));
                houseEntity.setState(2);
                houseService.updateById(houseEntity);
                map.put("state","success");
                map.put("message","恭喜你，预订成功!");
            } else {
                map.put("state","error");
                map.put("message","很抱歉，预订失败!");
            }
        }

        return R.ok().put("data",map);
    }
    public boolean checkTimeUsed(int id,String inDate,String outDate) throws ParseException {
        List<ApplyEntity> applyEntityList = applyService.getListByHouseId(id);

        if(applyEntityList.size() > 0){
            boolean res = false;
            for(ApplyEntity item : applyEntityList) {
                if(DateKit.isContain(inDate,outDate,item.getIndate(),item.getOutdate())) {
                    res = true;
                }
            }
            return res;
        }else {
            return false;
        }
    }
}
